package com.orangelife.mobile.parkingcard.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeHttpBiz;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.google.gson.reflect.TypeToken;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.common.activity.PullRefreshBaseActivity3;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.parkingcard.adapter.ParkingSpaceMgmtAdapter;
import com.orangelife.mobile.parkingcard.bean.ParkingListBean;
import com.orangelife.mobile.parkingcard.biz.ParkingSpaceHttpBiz;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 停车位管理
 * Created by  on 2016/7/5 0005.
 *
 * @author
 * @since v3.8
 */
public class ParkingSpaceManagementActivity extends PullRefreshBaseActivity3 {


    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;

    @Bind(R.id.plvList)
    PullToRefreshListView pullToRefreshListView;

    private List<ParkingListBean> mList = new ArrayList<>();
    private ParkingSpaceMgmtAdapter adapter;
    /*业主id*/
    private String ownerID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking_space_management);
        ButterKnife.bind(this);
        getIntentInfo();
        setAdapter();
        setListener();
        initTitle();
        setXListViewAndEmptyLayoutListener(pullToRefreshListView, emptyLayout, adapter);
//        pullToRefreshListView.setPullLoadEnable(true);
        requestList();
    }

    @Override
    protected boolean isDelayRequest() {
        return true;
    }

    private void getIntentInfo() {
        Intent intent = getIntent();
        ownerID = intent.getStringExtra("ownerID");
    }

    //    private ParkingSpaceMgmtAdapter getAdapter() {
//        if (adapter == null) {
//            adapter = new ParkingSpaceMgmtAdapter(this);
//        }
//        return adapter;
//    }
//private void setAdapter() {
//        adapter = new ParkingSpaceMgmtAdapter(this,ownerID);
//}
    private void setAdapter() {
        adapter = new ParkingSpaceMgmtAdapter(this);
    }

    private void setListener() {
        emptyLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                ???showProgress没有效果
//                showProgress();
                requestList();
            }
        });

    }

    private void initTitle() {
        tvTitle.setText("管理车位");
    }


    @Override
    protected void onResume() {
        super.onResume();

        showProgress();
        ParkingSpaceHttpBiz.requestParkingSpaceList1(ownerID, handler);
    }


    @Override
    protected EmptyLayout getEmptyLayout() {
        return emptyLayout;
    }

    @Override
    protected List getData() {
        return mList;
    }

    @Override
    protected int getDataCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    protected void requestWhenPullRefresh() {
        requestList();
    }


    private void requestList() {
        ParkingSpaceHttpBiz.requestParkingSpaceList(ownerID, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                dismissProgress();
                List<ParkingListBean> list = ParkingSpaceHttpBiz.handleParkingSpaceList(response);
                handleAfterRequest(isConnected(), OrangeHttpBiz.getTotalNum(response), list, pullToRefreshListView, adapter);
            }
        });
    }

    private android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject response = new JSONObject(String.valueOf(msg.obj));
                        List<ParkingListBean> list = OrangeDataHandler.getListFromJsonWithPageEntity(msg.obj, new TypeToken<List<ParkingListBean>>() {
                        }.getType(), null, null);
                        handleAfterRequest(isConnected(), OrangeHttpBiz.getTotalNum(response), list, pullToRefreshListView, adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
            }
        }
    };

}
